﻿using Flame.Dlr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Flame.Controls
{
    public class TextInputWithInfo : ATextInput
    {
        public class TextInputPropertyChangedEventArgs : EventArgs
        {
            public Panel InfoPanel { get; set; }
            public ATextInput TextInput { get; set; }
        }

        ATextInput _textInput;
        Panel _panel;

        const int infoPanelWidth = 50;

        public event EventHandler<TextInputPropertyChangedEventArgs> TextInputPropertyChanged;
        public override event EventHandler GotFocus;
        public override event System.Windows.Forms.KeyEventHandler KeyDown;
        public override event EventHandler AskForFocusToNext;
        public override event EventHandler AskForFocusToPrevious;
        public override event EventHandler OnMoveCaretOnScreen;
        public override event EventHandler OnResizeHeight;
        public override event EventHandler<CompilerResultEventArgs> OnCompile;
        public override event EventHandler<LanguageChangedEventArgs> OnLanguageChanged;
        public override event EventHandler<AutoStartChangedEventArgs> OnAutoStartChanged;
        public override event EventHandler<System.Windows.Forms.MouseEventArgs> MouseWheelFromChilds;

        public Panel InfoPanel { get; private set; }

        void _somethingChanged()
        {
            if (TextInputPropertyChanged != null)
            {
                TextInputPropertyChanged(this, new TextInputPropertyChangedEventArgs() { InfoPanel = InfoPanel, TextInput = _textInput });
            }
        }

        public TextInputWithInfo(ATextInput textInput)
        {
            _textInput = textInput;
            _panel = new Panel();

            _textInput.GotFocus += (s, e) => { if (GotFocus != null) GotFocus(s, e); };
            _textInput.KeyDown += (s, e) => { if (KeyDown != null) KeyDown(s, e); };

            _textInput.AskForFocusToNext += (s, e) => { if (AskForFocusToNext != null) AskForFocusToNext(s, e); };
            _textInput.AskForFocusToPrevious += (s, e) => { if (AskForFocusToPrevious != null) AskForFocusToPrevious(s, e); };
            _textInput.OnMoveCaretOnScreen += (s, e) => { if (OnMoveCaretOnScreen != null) OnMoveCaretOnScreen(s, e); };
            _textInput.OnResizeHeight += (s, e) => { if (OnResizeHeight != null) OnResizeHeight(s, e); };
            _textInput.OnCompile += (s, e) => { if (OnCompile != null) OnCompile(s, e); };
            _textInput.OnLanguageChanged += (s, e) => { _somethingChanged(); if (OnLanguageChanged != null) OnLanguageChanged(s, e); };
            _textInput.OnAutoStartChanged += (s, e) => { _somethingChanged(); if (OnAutoStartChanged != null) OnAutoStartChanged(s, e); };
            _textInput.MouseWheelFromChilds += (s, e) => { if (MouseWheelFromChilds != null) MouseWheelFromChilds(s, e); };

            _textInput.Control.Resize += (s, e) => { _panel.Size = _textInput.Control.Size; };


            _textInput.Control.Location = new System.Drawing.Point(infoPanelWidth, 0);
            _textInput.Control.Size = new System.Drawing.Size(_panel.Width - infoPanelWidth, _panel.Height);
            _textInput.Control.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left;
            _panel.Controls.Add(_textInput.Control);

            InfoPanel = new System.Windows.Forms.Panel();
            InfoPanel.Location = new System.Drawing.Point(0, 0);
            InfoPanel.Size = new System.Drawing.Size(infoPanelWidth, _textInput.Control.Height);
            InfoPanel.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left;
            _panel.Controls.Add(InfoPanel);
            InfoPanel.Controls.Clear();
            InfoPanel.Resize += (s, e) =>
            {
                InfoPanel.Height = _textInput.Control.Height + 100;
                //_textInput.Control.Left = _infoPanel.Width;
            };


            /*_panel.Paint += (s, e) =>
            {
                _somethingChanged();
            };
            InfoPanel.Paint += (s, e) =>
            {
                _somethingChanged();
            };*/
            /*_panel.Validated+=(s,e)=>{
            _somethingChanged();};*/
        }



        public override int CurrentPos { get { return _textInput.CurrentPos; } set { _textInput.CurrentPos = value; } }
        public override int CaretOnScreenPosY { get { return _textInput.CaretOnScreenPosY; } }
        public override int ContentHeight { get { return _textInput.ContentHeight; } }
        public override int caret_LineNumber { get { return _textInput.caret_LineNumber; } }
        public override int lines_Count { get { return _textInput.lines_Count; } }
        public override string selection_Text { get { return _textInput.selection_Text; } }
        public override string lines_Current_Text { get { return _textInput.lines_Current_Text; } }
        public override Control Control { get { return _panel; } }
        public override Common.IAutocomplete IAutocomplete { get { return _textInput.IAutocomplete; } }
        public override Common.ICallTip ICallTip { get { return _textInput.ICallTip; } }

        //protected override void onAutoStartChanged() { /*_somethingChanged(); _textInput.onAutoStartChanged();*/ }
        protected override void onLanguageChanged() { 
            /*_somethingChanged(); _textInput.onLanguageChanged(); */
        }



        public override Type Language { get { return _textInput.Language; } set { _textInput.Language = value; } }
        public override string Text { get { return _textInput.Text; } set { _textInput.Text = value; } }
        public override bool Focused { get { return _textInput.Focused; } }
        public override bool Focus() { return _textInput.Focus(); }

        public override void Compile() { _textInput.Compile(); }
        public override bool AutoStart { get { return _textInput.AutoStart; } set { _textInput.AutoStart = value; } }
        //public override void SetParent(System.Windows.Forms.Panel panel){_textInput.SetParent(panel);}
    }
}
